﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ChinaClipping.ClippingMaster.Controls
{
    public partial class MyBrowser : UserControl
    {
        protected class TabBorwser:TabPage
        {
            private WebBrowser _browser;
            public TabBorwser()
            {
                this._browser = new WebBrowser();
                this._browser.DocumentTitleChanged += new EventHandler(_browser_DocumentTitleChanged);
                this.Controls.Add(this._browser);
                this._browser.Dock = DockStyle.Fill;
            }

            void _browser_DocumentTitleChanged(object sender, EventArgs e)
            {
                this.Text = this.Browser.DocumentTitle;
            }

            public WebBrowser Browser
            {
                get { return this._browser; }
            }
        }

        private Stack<TabBorwser> _browserpool = new Stack<TabBorwser>();

        public event EventHandler CurrentURLChanged;

        public MyBrowser()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 新建页面
        /// </summary>
        public void NewBrowser()
        {
            createBrowser(); 
        }

        protected TabBorwser createBrowser()
        {
            TabBorwser tb;
            if (_browserpool.Count > 0)
                tb = _browserpool.Pop();
            else
                tb = new TabBorwser();
            tb.Browser.Navigate("about:blank");
            this.tabControl_Browsers.TabPages.Add(tb);
            this.tabControl_Browsers.SelectedTab = tb;
            if (this.tabControl_Browsers.TabPages.Count == 1)
            {
                tb.Browser.Navigated += new WebBrowserNavigatedEventHandler(Browser_Navigated);
                tb.Browser.StatusTextChanged += new EventHandler(Browser_StatusTextChanged);
                tb.Browser.ProgressChanged += new WebBrowserProgressChangedEventHandler(Browser_ProgressChanged);
                tb.Browser.NewWindow += new CancelEventHandler(Browser_NewWindow);
            }

            return tb;
        }

        /// <summary>
        /// 双击关闭选项卡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tabControl_DoubleClick(object sender, EventArgs e)
        {
            TabBorwser tb=this.tabControl_Browsers.SelectedTab as TabBorwser ;
            tb.Browser.Navigate("about:blank");
            this.tabControl_Browsers.TabPages.Remove(tb);
            this._browserpool.Push(tb);
            OnCurrenURLChanged();
        }

        /// <summary>
        /// 获取当前浏览器
        /// </summary>
        public WebBrowser ActiveBrowser
        {
            get 
            {
                if (this.tabControl_Browsers.SelectedTab != null)
                {
                    TabBorwser tb = this.tabControl_Browsers.SelectedTab as TabBorwser;
                    if (tb != null)
                    {
                        return tb.Browser;
                    }
                    else
                    {
                        return null;
                    }

                }
                else
                {
                    return null;
                }
            
            }
        
        }

        /// <summary>
        /// 显示指定HTML内容
        /// </summary>
        /// <param name="html"></param>
        public void SetHtml(string html)
        {
            WebBrowser wb;
            if (ActiveBrowser != null && ActiveBrowser.Url.ToString() == "about:blank")
            {
                wb = ActiveBrowser;
            }
            else
            {
                wb = createBrowser().Browser;
            }
            wb.DocumentText = html;
        }

        /// <summary>
        /// 在当前浏览器中打开指定URL
        /// </summary>
        /// <param name="url"></param>
        public void Navigate(string url)
        {
            if (ActiveBrowser == null)
             NewBrowser();
            if (ActiveBrowser != null)
                ActiveBrowser.Navigate(url);
        }

        /// <summary>
        /// 停止导航
        /// </summary>
        public void Stop()
        {
            if (ActiveBrowser != null)
                ActiveBrowser.Stop();
        }
        /// <summary>
        /// 刷新
        /// </summary>
        public void refresh()
        {
            if (ActiveBrowser != null)
                ActiveBrowser.Refresh();
        }
        /// <summary>
        /// 显示保存对话框
        /// </summary>
        public void ShouSaveAsDialog()
        {
            if (ActiveBrowser != null)
                ActiveBrowser.ShowSaveAsDialog();
        }
        /// <summary>
        /// 在外部浏览器中打开当前页
        /// </summary>
        public void OpenUrlInIE()
        {
            if (ActiveBrowser != null && ActiveBrowser.Url.ToString () != "about:blank")
                System.Diagnostics.Process.Start(ActiveBrowser.Url.ToString ());
        }

        /// <summary>
        /// 后退
        /// </summary>
        public void GoBack()
        {
            if (ActiveBrowser != null)
                ActiveBrowser.GoBack();
        }
        /// <summary>
        /// 前进
        /// </summary>
        public void GoForward()
        {
            if (ActiveBrowser != null)
                ActiveBrowser.GoForward();
        }

        private void tabControl_Browsers_Deselected(object sender, TabControlEventArgs e)
        {
            TabBorwser tb = e.TabPage as TabBorwser;
            if (tb != null)
            {
                tb.Browser.Navigated -= Browser_Navigated;
                tb.Browser.StatusTextChanged -= Browser_StatusTextChanged;
                tb.Browser.ProgressChanged -=Browser_ProgressChanged;
                tb.Browser.NewWindow -= Browser_NewWindow;
            }
        }

        private void tabControl_Browsers_Selected(object sender, TabControlEventArgs e)
        {
            TabBorwser tb = e.TabPage as TabBorwser;
            if (tb != null)
            {
                tb.Browser.Navigated -= Browser_Navigated;
                tb.Browser.StatusTextChanged -= Browser_StatusTextChanged;
                tb.Browser.ProgressChanged -= Browser_ProgressChanged;
                tb.Browser.NewWindow -= Browser_NewWindow;
                tb.Browser.Navigated += new WebBrowserNavigatedEventHandler(Browser_Navigated);
                tb.Browser.StatusTextChanged += new EventHandler(Browser_StatusTextChanged);
                tb.Browser.ProgressChanged += new WebBrowserProgressChangedEventHandler(Browser_ProgressChanged);
                tb.Browser.NewWindow += new CancelEventHandler(Browser_NewWindow);
                OnCurrenURLChanged();
            }
        }

        void Browser_NewWindow(object sender, CancelEventArgs e)
        {
            TabBorwser tb = createBrowser();
            tb.Browser.Navigate((sender as WebBrowser).StatusText);
            e.Cancel = true;
        }

        void Browser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            TSPB_Progress.Maximum = (int)e.MaximumProgress;
            TSPB_Progress.Value = (int)e.CurrentProgress;
        }

        void Browser_StatusTextChanged(object sender, EventArgs e)
        {
            TSSL_Status.Text = ActiveBrowser.StatusText;
        }

        void Browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            OnCurrenURLChanged();
        }

        private void OnCurrenURLChanged()
        {
            EventHandler eh = this.CurrentURLChanged;
            if (eh != null)
                eh(this, new EventArgs());
        }

      

    }
}
